<?php

namespace App\Kuafu\Complain\Controller\B;

use App\Kuafu\Base\Enum\BaseCodeEnum;
use App\Kuafu\Complain\Service\ComplainService;
use App\Kuafu\Complain\Validate\ComplainValidate;
use Exception;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Kuafu\Base\Controller\BaseController;

class ComplainController extends BaseController
{

    /**
     * 小程序-投诉工单列表
     * @param Request $request
     * @return array|JsonResponse
     */
    public function list(Request $request)
    {
        $status            = $request->get('status', "");
        $orderNo           = (string)$request->get('orderNo', "");
        $submitTimeStart   = (string)$request->get("submitTimeStart", "");
        $submitTimeEnd     = (string)$request->get("submitTimeEnd", "");
        $completeTimeStart = (string)$request->get("completeTimeStart", "");
        $completeTimeEnd   = (string)$request->get("completeTimeEnd", "");
        $problemPid        = (int)$request->get('problemPid', '');
        $problemChildId    = (string)$request->get('problemChildId', '');
        $pageSize          = (int)$request->get('pageSize', 10);
        $pageNum           = (int)$request->get('pageNum', 1);
        $export            = (bool)$request->get("export", false);

        $data = ComplainService::ComplainLists($status, $orderNo, $submitTimeStart, $submitTimeEnd, $completeTimeStart, $completeTimeEnd, $problemPid, $problemChildId, $pageSize, $pageNum, $export);
        return $this->success($data);
    }


    /**
     * get info
     * @param Request $request
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function info(Request $request): JsonResponse
    {
        if (empty($request->id)) {
            return $this->fail(BaseCodeEnum::ERR_400000, 'id不能为空');
        }
        $data = ComplainService::getComplainInfo($request);
        return $this->success($data);
    }

    /**
     * 修改小程序-投诉工单
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function update(Request $request): JsonResponse
    {
        //参数校验
        $valid = ComplainValidate::updateComplain($request);
        if ($valid) {
            return $this->fail(BaseCodeEnum::ERR_400000, $valid);
        }
        if (empty($request->id)) {
            return $this->fail(BaseCodeEnum::ERR_400000, 'id不能为空');
        }
        $userId = auth('api')->user()->id;
        ComplainService::updateComplain($request, $userId);
        return $this->success();
    }

    /**
     * 获取小程序投诉问题列表
     * @return JsonResponse
     */
    public function issueList(): JsonResponse
    {
        return $this->success(ComplainService::getIssueList());
    }
}
